﻿#region

using System;
using System.Collections.Generic;
using Tateeda.ClinicalStudy.Base.Data.DBModel;
using Tateeda.ClinicalStudy.Base.Data.EF;
using Tateeda.ClinicalStudy.Base.Modules.Common;
using Tateeda.ClinicalStudy.Base.Modules.Infrastructure.Interfaces.Services;
using Tateeda.ClinicalStudy.Base.Modules.Models.Study;
using Tateeda.ClinicalStudy.Base.Modules.Models.StudyVisit;
using Tateeda.ClinicalStudy.Base.Modules.Models.User;
using System.Linq;
using System.Data.Entity;
using Tateeda.ClinicalStudy.Base.Modules.Common.Extenstions;

#endregion

#region - Legal -

///<legal>
/// Copyright © 2011, Tateeda Media Network
/// All rights reserved. http://tateeda.com
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions
/// are met:
///
/// - Redistributions of source code must retain the above copyright
/// notice, this list of conditions and the following disclaimer.
///
/// - Neither the name of the Tateeda Media Network, nor the names of its
/// contributors may be used to endorse or promote products
/// derived from this software without specific prior written
/// permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
/// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
/// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
/// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
/// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
/// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
/// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
/// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
/// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
///</legal>

#endregion - Legal -

namespace Tateeda.ClinicalStudy.Base.Modules.Infrastructure.Concrete.Services {

    public class AdminService : IAdminService {
        private readonly SqlUnitOfWork _unitOfWork;

        public AdminService() {
            _unitOfWork = new SqlUnitOfWork();
        }

        #region - IAdminService Members -

        public virtual IUnitOfWork UnitOfWork {
            get { return _unitOfWork; }
        }

        public void CreateVisitStep(VisitStepModel step) {
            var newStep = new VisitStep { Description = step.Description, IsActive = step.IsActive, Name = step.Name };
            UnitOfWork.VisitSteps.AddObject(newStep);
            UnitOfWork.Commit();
        }

        public void CreateVisitStepSequence(ICollection<VisitStepVisitSequenceModel> sequece) {
            var newSeq = new VisitStepVisitSequence();
            Util.CopyFromModel(sequece, ref newSeq);
            UnitOfWork.VisitStepVisitSequences.AddObject(newSeq);
            UnitOfWork.Commit();
        }

        public void DeleteVisitSetepSequence(int visitId, int stepId) {
            var step = UnitOfWork.VisitStepVisitSequences.Where(vs => vs.VisitId == visitId && vs.VisitStepId == stepId).FirstOrDefault();
            UnitOfWork.VisitStepVisitSequences.DeleteObject(step);
            UnitOfWork.Commit();
        }

        public void DeleteVisitStep(int stepId) {
            var step = UnitOfWork.VisitSteps.Where(vs => vs.VisitStepId == stepId).FirstOrDefault();
            UnitOfWork.VisitSteps.DeleteObject(step);
            UnitOfWork.Commit();
        }

        public ICollection<VisitStepModel> GetAllVisitSteps() {
            return UnitOfWork.VisitSteps.ToList().ToVisitStepModelList();
        }

        public bool OverrideAppointment(AppUserModel adminUser, AppointmentModel oldAppointment, AppointmentModel newAppointment) {
            throw new NotImplementedException();
        }

        public ICollection<VisitStepVisitSequenceModel> GetVisitStepVisitsSequence(int stepId) {
            var sequence = UnitOfWork.VisitStepVisitSequences.Where(vs => vs.VisitStepId == stepId).ToList();
            return sequence.ToVisitStepVisitSequenceModelList();
        }

        public ICollection<int> GetVisitStepAvailableVisitsMap(int stepId) {
            var visits = UnitOfWork.Visits.Where(v => v.VisitSteps.Any(s => s.VisitStepId == stepId && s.IsActive)).ToList();
            return visits.Select(v => v.VisitId).ToList();
        }

        public bool UpdateVisitStep(VisitStepModel step) {
            var curStep = UnitOfWork.VisitSteps.Where(vs => vs.VisitStepId == step.VisitStepId).FirstOrDefault();
            Util.CopyFromModel(step, ref curStep);
            UnitOfWork.Commit();

            return true;
        }

        public bool UpdateVisitStepSequence(VisitStepVisitSequenceModel sequenceModel) {
            var editSeq = UnitOfWork.VisitStepVisitSequences.Where(vs => vs.VisitId == sequenceModel.VisitId && vs.VisitStepId == sequenceModel.VisitStepId).FirstOrDefault();
            Util.CopyFromModel(sequenceModel, ref editSeq);
            UnitOfWork.Commit();
            return true;
        }

        public ICollection<VisitStepVisitSequenceModel> GetAllVisitsSequences() {
            return UnitOfWork.VisitStepVisitSequences.Include("Visit").ToList().ToVisitStepVisitSequenceModelList();
        }

        #endregion
    }
}